﻿using System;
using Communication.Message.Buffer;
using growatt.Message.Util;

namespace Communication.Message.Osc
{
    /// <summary>
    /// 地址设置报文处理
    /// </summary>
    public class AddrSetChannelMessage : OscChannelMessage
    {
        public AddrSetChannelMessage(int[] addrs,byte ScopeMode,byte datatype,byte waveRatio1, byte waveRatio2) : base(0x10)
        {
            try
            {
                this.channelCount = addrs.Length;
                this.sendBuffer = MessageBuffers.Buffer(this.channelCount * 4  + 4 + 3);
                this.receiveBuffer = MessageBuffers.DynamicBuffer(this.channelCount * 4 + 4 + 3);
                this.sendBuffer.WriteByte(byte.MaxValue);
                this.sendBuffer.WriteByte(this.fun);
                for (int i = 0; i < addrs.Length; i++)
                {
                    this.sendBuffer.WriteInt(addrs[i]);
                }
               
                this.sendBuffer.WriteByte(ScopeMode);
                this.sendBuffer.WriteByte(datatype);
                this.sendBuffer.WriteByte(waveRatio1);
                this.sendBuffer.WriteByte(waveRatio2);
                byte b = OscUtils.Crc8(this.sendBuffer.Array(), this.sendBuffer.ReaderIndex(), this.sendBuffer.ReadableBytes());
                this.sendBuffer.WriteByte(b);
                var str = ConvertHelper.ByteToHexStr(sendBuffer.Array(), joinString: " ");
                Console.WriteLine(str + $"   长度{sendBuffer.Array().Length}");
                this.sendBuffer.MarkReaderIndex();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        /// 处理接收报文
        /// </summary>
        /// <returns></returns>
        protected override bool DealwithReceiveMsg()
        {
            byte @byte;
            byte b;
            if (this.receiveBuffer.ReadableBytes() == 3)
            {
                @byte = this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 2);
                b = OscUtils.Crc8(this.receiveBuffer.Array(), this.receiveBuffer.ReaderIndex(), 2);
                if (b == @byte)
                {
                    if (Enum.IsDefined(typeof(OscErrorCode), (uint)this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 1)))
                    {
                        this.errorCode = (OscErrorCode)Enum.Parse(typeof(OscErrorCode), this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 1).ToString());
                    }
                    base.SetSuccess();
                    return true;
                }
            }
            if (this.receiveBuffer.ReadableBytes() < this.channelCount * 4 + 3 + 4)
            {
                return false;
            }
            @byte = this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + this.channelCount * 4 + 2 + 4);
            b = OscUtils.Crc8(this.receiveBuffer.Array(), this.receiveBuffer.ReaderIndex(), this.channelCount * 4 + 2 + 4);
            if (b != @byte)
            {
                this.SetFailure(new CrcRrrorException());
                return true;
            }
            this.sendBuffer.ResetReaderIndex();//重置
            if (!this.receiveBuffer.Equals(this.sendBuffer))
            {
                this.SetFailure(new InvalidOscMessageException("send and receive message mismatch"));
                return true;
            }
            this.errorCode = OscErrorCode.SUCCESS;
            base.SetSuccess();
            return true;
        }

        // Token: 0x06000146 RID: 326 RVA: 0x00003BD4 File Offset: 0x00001DD4
        public override int GetCancelSendData(byte[] buffer, int offset)
        {
            return 0;
        }

        // Token: 0x0400004B RID: 75
        private int channelCount;
    }
}
